<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>车管所新增</title>
    <link href="../../layui/css/layui.css" rel="stylesheet">
    <link href="../../static/css/style.css" rel="stylesheet">
    <script src="../../static/js/jquery.min.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../static/js/base.js"></script>
    <script src="../../static/js/ax.js"></script>
    <script src="../../static/js/jquery.cookie.js"></script>
    <script src="../../static/js/avalon.js"></script>
    <!--[if lt IE 9]>
    <script src="../../static/js/html5.js"></script>
    <script src="../../static/js/respond.js"></script>
    <![endif]-->

</head>
<body class="bg_w">

<div class="content-wrap" ms-controller="test" >
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-row layui-col-space10 layui-form-item">

                <div class="layui-col-md4">
                    <label class="layui-form-label">省份：</label>
                    <div class="layui-input-block">
                        <select  id="provinceId" name="provinceId" lay-verify="required" lay-filter="provinceSelect">
                        </select>
                    </div>
                </div>
                <div class="layui-col-md4">
                    <label class="layui-form-label">市辖：</label>
                    <div class="layui-input-block">
                        <select id="cityId" name="cityId" lay-verify="required" lay-filter="aihao">
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-space10 ">
                <div class="layui-row ">

                    <label class="layui-form-label">对应分所：</label>
                    <div class="layui-input-block">
                        <a class="layui-btn add_btn" id="add">
                            <i class="layui-icon">&#xe608;</i> 添加
                        </a>
                    </div>

                </div>
            </div>


            <div class="layui-form-item cgs-list" id="cgs_addlist">



            </div>

            <div class="layui-form-item layui-col-space10">
                <div class="layui-input-block">
                    <button class="layui-btn add_btn" lay-submit lay-filter="component-form-element">立即提交</button>
                    <button ms-click="argsClick()" type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>


</body>

<script>
 
    // ax.post({
    //             url: '/api/captcha/getRegionProvince',
    //             contentType: "application/x-www-form-urlencoded",
    //             data:"",
    //             success: function (r) {
                   
    //             },
    //         });

    $(function () {
        addRow();
        //获取总校
        $(function () {
            ax.post({
                url: schoolWebUrl+'/api/captcha/getRegionProvince',
                contentType: "application/x-www-form-urlencoded",
                data:"",
                success: function (r) {
                    for(var i=0;i<r.data.length;i++){
                        $("#provinceId").append(' <option value='+r.data[i].code+'>'+r.data[i].name+'</option>');
                    }
                    //默认为第一个为北京市市
                    ax.post({
                        url: schoolWebUrl+'/api/captcha/getRegionCode',
                        contentType: "application/x-www-form-urlencoded",
                        data:{"code": 110000},
                        success: function (r) {
                            for(var i=0;i<r.data.length;i++){
                                $("#cityId").append(' <option value='+r.data[i].code+'>'+r.data[i].name+'</option>');
                            }
                            setTimeout(function () {
                                layui.form.render('select');
                            },200)
                        },
                    });
                },
            });
        });

    })



    $('#add').click(function () {
        addRow();
    });

    function addRow(){
        $('#cgs_addlist').append('<div class="cgsaddlist layui-row layui-col-space10">\n' +
            '                    <div class="layui-col-sm4">\n' +
            '                        <label class="layui-form-label">分所代码：</label>\n' +
            '                        <div class="layui-input-block">\n' +
            '                            <input type="text" name="code" placeholder="请输入分所代码" autocomplete="off" class="layui-input code">\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                    <div class="layui-col-sm4">\n' +
            '                        <label class="layui-form-label">分所名称：</label>\n' +
            '                        <div class="layui-input-block">\n' +
            '                            <input type="text" name="name" placeholder="请输入分所名称" autocomplete="off" class="layui-input name">\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                    <div class="layui-col-sm4">\n' +
            '\n' +
            '\n' +
            '                        <button class="layui-btn layui-btn-danger remover_btn mleft">\n' +
            '                            删除\n' +
            '                        </button>\n' +
            '\n' +
            '                    </div>\n' +
            '\n' +
            '                </div>');
        $('#cgs_addlist .remover_btn').bind('click',function(){
            $(this).parents('.cgsaddlist').remove();
        });
    }
    var table;

    layui.use(['table','form','laydate','layer','upload'], function() {
        table = layui.table;
        var form = layui.form,
            laydate = layui.laydate,
            layer=layui.layer,
            upload = layui.upload,
            $ = layui.jquery;

        //常规用法
        laydate.render({
            elem: '#activeTime'
            ,range: true
        });

        form.on('select(provinceSelect)', function(data){
            ax.post({
                url:  schoolWebUrl+'/api/captcha/getRegionCode',
                contentType: "application/x-www-form-urlencoded",
                data:{"code": data.value},
                success: function (r) {
                    console.log(r);
                    $("#cityId").empty();
                    for(var i=0;i<r.data.length;i++){
                        $("#cityId").append(' <option value='+r.data[i].code+'>'+r.data[i].name+'</option>');
                    }
                    layui.form.render('select');
                },
            });
        });


        //监听提交
        form.on('submit(component-form-element)', function(data){
            var provinceId=data.field.provinceId;
            var cityId=data.field.cityId;
            var postData=[];
            var j=0;
            $.each($('#cgs_addlist .cgsaddlist'),function(i,n){
                var code=$('.code').eq(i).val();
                var name=$('.name').eq(i).val();
                if(code==""||name==""){
                    layer.msg("请填写完成");
                    return;
                }
                postData[j++] = {
                    cityId:cityId,
                    provinceId:provinceId,
                    code: code,
                    name: name,
                }
            });
            var index = layer.load(0, {shade: false});
            ax.post({
                type: 'POST',
                url: schoolWebUrl+'/api/division/add',
                autoPrefix: false,
                contentType: "application/json",
                data: JSON.stringify(postData),
                success: function (data) {
                    layer.alert(data.message);
                    layer.close(index);
                    if (data.code == 0) {
                        //刷新父页面
                        setTimeout( function(){
                            topFrame.location.reload();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }, 2 * 1000 );//延迟2000毫米
                    }
                },
            });
            return false;
        });
    });
    //重置页面
    function reset() {
        //执行重载
        table.reload('testReload', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
            }
        });
    }
</script>
</html>